<?php

namespace App\Http\Controllers\Demand;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class CreateController extends Controller
{
    public function index()
    {

        return view('demand.create',[
            'clique'=>DB::table('business')->where(['Type'=>2,'Del'=>1])->get(),
            'demandtype_ued_list'=>DB::table('pacttype')->where(['Type'=>2,'Del'=>1])->select('id','Name','UedWorktime')->orderBy('PactId')->orderBy('Inx','desc')->get(),
            'demandtype_dev_list'=>DB::table('pacttype')->where(['Type'=>4,'Del'=>1])->select('id','Name','KWorktime')->orderBy('PactId')->orderBy('Inx','desc')->get(),
            'demandtype_combo_list'=>DB::table('pacttype')->where(['Type'=>6,'Del'=>1])->select('id','Name','UedWorktime','KWorktime')->orderBy('PactId')->orderBy('Inx','desc')->get(),
        ]);
    }

    //层级菜单（根据集团搜索获取所有后代）
    public function tier(Request $request)
    {
        function array_function($type,$pid)
        {
            $ary=DB::table('business')->where(['Type'=>$type,'CliqueId'=>$pid,'Del'=>1])->select('id','Name','PId')->get();
            foreach($ary as $val)
            {
                $data[$val->PId][]=$val;
            }
            return $data;
        }
        function check_child($type,$pid)
        {
            $data=DB::table('business')->where(['Type'=>$type,'CliqueId'=>$pid,'Del'=>1])->get();
            if($data==NULL)
            {
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
        $data=array();
        if(check_child(4,$request->input('clique_id')))
        {
            $data['com']=array_function(4,$request->input('clique_id'));
        }
        if(check_child(6,$request->input('clique_id')))
        {
            $data['dep']=array_function(6,$request->input('clique_id'));
        }
        if(check_child(8,$request->input('clique_id')))
        {
            $data['group']=array_function(8,$request->input('clique_id'));
        }
        return json_encode($data);
    }

    //正则匹配需求方(类型1)/接口人(类型2)姓名
    public function regexp_search(Request $request)
    {
        $name=trim($request->input('name'));
        switch($request->input('type'))
        {
            case 1:
            $val=DB::table('operate')->where('Name','regexp',$name)->where('Del',1)->select('id','Name')->get();
            return json_encode($val);
            break;
            case 2:
            $val=DB::table('operate')->where('Name','regexp',$name)->where('Del',1)->select('id','Name')->get();
            return json_encode($val);
            break;
            default:
            return 0;
        }
    }

    //创建需求
    public function create(Request $request)
    {
        //**********需求表写入开始**********//
        //解析demand字符串为数组
        $demand=unserialize($request->input('demand'));
        $org=explode(',', $demand['org']);
        //判断需求类型
        switch($demand['type'])
        {
            case '1':
            $demand_type=1;
            break;
            case '2':
            $demand_type=2;
            break;
            case '3':
            $demand_type=3;
            break;
            default:
            return 0;
        }
        //判断是否有备注信息
        if($demand['text']==0)
        {
            $text=NULL;
        }
        else
        {
            $text=$demand['text'];
        }
        //判断是否有阿里编号
        if($demand['alikey']==0)
        {
            $alikey=0;
        }
        else
        {
            $alikey=$demand['alikey'];
        }
        //写入需求内容
        $demand_insert=array(
            'CliqueId'=>$org[0],
            'ComId'=>$org[1],
            'DepId'=>$org[2],
            'GroupId'=>$org[3],
            'OperateId'=>$demand['operate_id'],
            'UseId'=>session('user')[0]->id,
            'Name'=>$demand['name'],
            'LeaderId'=>$demand['leader_id'],
            'Alikey'=>$alikey,
            'Type'=>$demand_type,
            'Via'=>5,
            'Date'=>$this->settlement(),
            'Text'=>$text
        );
        $demand_id=DB::table('demand')->insertGetId($demand_insert);
        //**********需求表写入结束**********//
        //**********需求类型表开始**********//
        //解析demand字符串为数组
        $demand_type=unserialize($request->input('demand_type'));
        //写入需求类型内容
        //UED写入开始
        $ued=array_filter($demand_type,function($var){ return DB::table('pacttype')->where('id',$var[0])->pluck('Type')=='2';});
        foreach($ued as $val)
        {
            $type_ued_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>2,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_ued_insert);
        //UED写入结束
        //DEV写入开始
        $dev=array_filter($demand_type,function($var){ return DB::table('pacttype')->where('id',$var[0])->pluck('Type')=='4';});
        foreach($dev as $val)
        {
            $type_dev_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>4,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('KWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('KCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_dev_insert);
        //DEV写入结束
        //套餐写入开始
        //套餐-UED写入开始
        $combo_ued=array_filter($demand_type,function($var){ return $var[2]=='2';});
        foreach($combo_ued as $val)
        {
            $type_combo_ued_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>6,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_combo_ued_insert);
        //套餐-UED写入结束
        //套餐-DEV写入开始
        $combo_dev=array_filter($demand_type,function($var){ return $var[2]=='4';});
        foreach($combo_dev as $val)
        {
            $type_combo_dev_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>6,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('KWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('KCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_combo_dev_insert);
        //套餐-DEV写入结束
        //套餐写入结束
        //**********需求类型表结束**********//
        //**********图片素材表开始**********//
        
        //**********图片素材表结束**********//
    }

    public function store(Request $request)
    {
        //
    }

    public function show($id)
    {
        //
    }

    //修改需求
    // $data=[1=>['a','aa'],2=>['b','bb'],3=>['c','cc'],4=>['d','dd'],5=>['e','ee']];
        // $ids = implode(',', array_keys($data));
        // $sql="UPDATE proc_test SET name= CASE id ";
        // foreach ($data as $id => $val) { 
        // $sql .= sprintf("WHEN %d THEN '%s' ", $id, $val[0]); 
        // }
        // $sql .="END,val= CASE id ";
        // foreach ($data as $id => $val) { 
        // $sql .= sprintf("WHEN %d THEN '%s' ", $id, $val[1]); 
        // }
        // $sql .="END WHERE id IN ($ids)";
        // DB::select($sql);
    public function edit(Request $request)
    {
        if($request->has('demand_id'))
        {
            $demand_id=$request->input('demand_id');
        //**********需求表写入开始**********//
        //解析demand字符串为数组
        $demand=unserialize($request->input('demand'));
        $org=explode(',', $demand['org']);
        //判断需求类型
        switch($demand['type'])
        {
            case '1':
            $demand_type=1;
            break;
            case '2':
            $demand_type=2;
            break;
            case '3':
            $demand_type=3;
            break;
            default:
            return 0;
        }
        //判断是否有备注信息
        if($demand['text']==0)
        {
            $text=NULL;
        }
        else
        {
            $text=$demand['text'];
        }
        //判断是否有阿里编号
        if($demand['alikey']==0)
        {
            $alikey=0;
        }
        else
        {
            $alikey=$demand['alikey'];
        }
        //写入需求内容
        $demand_insert=array(
            'CliqueId'=>$org[0],
            'ComId'=>$org[1],
            'DepId'=>$org[2],
            'GroupId'=>$org[3],
            'OperateId'=>$demand['operate_id'],
            'UseId'=>session('user')[0]->id,
            'Name'=>$demand['name'],
            'LeaderId'=>$demand['leader_id'],
            'Alikey'=>$alikey,
            'Type'=>$demand_type,
            'Via'=>5,
            'Date'=>$this->settlement(),
            'Text'=>$text
        );
        //$demand_id=DB::table('demand')->insertGetId($demand_insert);
        //**********需求表写入结束**********//
        //**********需求类型表开始**********//
        //解析demand字符串为数组
        $demand_type=unserialize($request->input('demand_type'));
        //写入需求类型内容
        //数据格式[['1','12',2],['id','num','type]]
        //UED写入开始
        $ued=array_filter($demand_type,function($var){ return $val[2]=='2';});
        $ued_ids=implode(',', array_keys($ued));
        //$ued_sql="UPDATE ddemandtype SET DemandId=($demand_id),";
        foreach($ued as $val)
        {
            array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>2,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_ued_insert);
        //UED写入结束
        //DEV写入开始
        $dev=array_filter($demand_type,function($var){ return $val[2]=='4';});
        foreach($dev as $val)
        {
            $type_dev_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>4,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('KWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('KCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_dev_insert);
        //DEV写入结束
        //套餐写入开始
        //套餐-UED写入开始
        $combo_ued=array_filter($demand_type,function($var){ return $var[2]=='6';});
        foreach($combo_ued as $val)
        {
            $type_combo_ued_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>6,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('UedPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_combo_ued_insert);
        //套餐-UED写入结束
        //套餐-DEV写入开始
        $combo_dev=array_filter($demand_type,function($var){ return $var[2]=='7';});
        foreach($combo_dev as $val)
        {
            $type_combo_dev_insert[]=array('DemandId'=>$demand_id,'PactId'=>DB::table('pacttype')->where('id',$val[0])->pluck('PactId'),'PactTypeId'=>$val[0],'Nums'=>$val[1],'PactType'=>6,'Price'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice'),'Worktime'=>DB::table('pacttype')->where('id',$val[0])->pluck('KWorktime'),'ComPrice'=>DB::table('pacttype')->where('id',$val[0])->pluck('KCommission')*$val[1],'UseId'=>session('user')[0]->id,'PactName'=>DB::table('pact')->where('id',DB::table('pacttype')->where('id',$val[0])->pluck('PactId'))->pluck('Name'),'PactTypeName'=>DB::table('pacttype')->where('id',$val[0])->pluck('Name'),'SumPric'=>DB::table('pacttype')->where('id',$val[0])->pluck('KPrice')*$val[1]);
        }
        DB::table('ddemandtype')->insert($type_combo_dev_insert);
        //套餐-DEV写入结束
        //套餐写入结束
        //**********需求类型表结束**********//
        //**********图片素材表结束**********//
        
        //**********图片素材表结束**********//
        }
        else
        {
            return 0;
        }
    }

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }

    //结算日期算法
    public function settlement()
    {
        $date=date('Ymd',time());
        $date_judge=substr($date, 4);
        if(126<=$date_judge&&$date_judge<=1225)
        {
            if($date_judge<=substr($date_judge,0,2).'25')
            {
                return substr($date, 0,6);
            }
            else
            {
                return substr($date, 0,6)+1;
            }
        }
        else
        {
            if(1226<=$date_judge)//年位+1
            {
                return (substr($date, 0,4)+1).'01';
            }
            else
            {
                return substr($date, 0,4).'01';
            }
        }
    }

    //update批量修改高效方法
    public function edit_update()
    {

    }

    public function test()
    {
        $data=[1=>['a','aa'],2=>['b','bb'],3=>['c','cc'],4=>['d','dd'],5=>['e','ee']];
        $ids = implode(',', array_keys($data));
        $sql="UPDATE proc_test SET name= CASE id ";
        foreach ($data as $id => $val) { 
        $sql .= sprintf("WHEN %d THEN '%s' ", $id, $val[0]); 
        }
        $sql .="END,val= CASE id ";
        foreach ($data as $id => $val) { 
        $sql .= sprintf("WHEN %d THEN '%s' ", $id, $val[1]); 
        }
        $sql .="END WHERE id IN ($ids)";
        DB::select($sql);
        dump($sql);
    }

}
